package com.bishe.controller;

import com.bishe.entity.Project;
import com.bishe.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("project")
public class ProjectController {
    //注入对象
    @Autowired
    ProjectService projectService;

    //首页
    @RequestMapping("index1")
    public String index1(){
        return "redirect:/index.jsp";
    }

    /**
     * 页面跳转
     * @return  添加项目页面
     */
    @RequestMapping("addProjectPage")
    public String addProjectPage(){
        return "redirect:/extractSystem/addProject.jsp";
    }

    /**
     * 添加一个新的project项目
     * @param project 从jsp页面获取的project对象
     * @return 页面跳转至项目列表
     */
    @RequestMapping("addProject")
    public String addProject(Project project){
        //设置项目状态，1表示可见，0表示不可见,用于删除功能的实现
        project.setStatus(1);
        projectService.addProject(project);
        return "redirect:projectList";
    }

    /**
     * 分页查询数据库中的数据
     * @param page  当前页码
     * @param limit 每页记录数
     * @param request  jsp页面通过request获取list，page，pagecount
     * @return  返回跳转的页面
     */
    @RequestMapping("projectList")
    public String selectBypage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5")  int limit,
            HttpServletRequest request
    ){
        /*
         * 需要的数据  当前第page页，共pageCount页
         * */
        //从数据库获取数据
        List<Project> list =projectService.selectProjectInfoByPage(page,limit);
        //获取总页数
        Long pageCount = projectService.getPageCount(limit);
        request.setAttribute("list",list);
        request.setAttribute("page",page);
        request.setAttribute("pageCount",pageCount);
        return "forward:/extractSystem/projectList.jsp";
    }

    /**
     * 根据id查询项目详情
     * @param request
     * @param id
     * @return 返回值项目详情页
     */
    @RequestMapping("projectDetails")
    public String selectProjectDetails(HttpServletRequest request,Integer id){
        Project project = projectService.selectProjectDetails(id);
        request.setAttribute("project",project);
        return "forward:/extractSystem/projectDetails.jsp";
    }

    /**
     * 根据id删除数据（假删除,实际为编辑操作，将status置为0）
     * @param id
     * @return 跳转至projectList,再次进行查询
     */
    @RequestMapping("deleteProject")
    public String deleteProjectById(Integer id){
            projectService.deleteProjectById(id);
            return "redirect:projectList";
    }

    /**
     * 批量删除（假删除）功能
     * @param ids
     * @return productList，删除后刷新页面
     */
    @RequestMapping("deleteMany")
    public String deleteMany(Integer[] ids){
        if (ids==null){
            return "redirect:projectList";
        }else{
            projectService.deleteMany(ids);
            return "redirect:projectList";
        }

    }

    /**
     * 根据id查询要编辑的项目信息，转发至projectEdit页面
     * @param request 在projectEdit页面通过request获取本来的属性
     * @param id
     * @return 返回值
     */
    @RequestMapping("selectForProjectUpdate")
    public String selectForProjectUpdate(HttpServletRequest request,Integer id){
        Project project = projectService.selectProjectDetails(id);
        request.setAttribute("project",project);
        return "forward:/extractSystem/projectEdit.jsp";
    }

    /**
     * 编辑完成后返回项目列表页面
     * @param project
     * @return
     */
    @RequestMapping("projectUpdate")
    public String projectUpdate(Project project){
        projectService.projectUpdate(project);
        System.out.println(project.getPname());
        return"redirect:projectList";
    }



}
